package com.yidu.best.TA.Ta.service.impl;

import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.yidu.best.TA.Fundinventory.domain.Fundinventory;
import com.yidu.best.TA.Ta.dao.TaDao;
import com.yidu.best.TA.Ta.domain.Ta;
import com.yidu.best.TA.Ta.domain.TaVo;
import com.yidu.best.TA.Ta.service.TaService;
import com.yidu.best.TA.Ta.util.ExcelUtil;
import com.yidu.best.TA.Ta.util.IDUtil;
import com.yidu.best.basicSetup.fundset.domain.Fund;
import com.yidu.best.bond.cashInventory.domain.CashInventory;
import com.yidu.best.bond.clear.domain.CapitalTransfer;

@Service
public class TaServiceImpl implements TaService {
	
	@Autowired
	private TaDao taDao;

	@Override
	public Map<String, Object> findTa(TaVo taVo) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = taDao.findCountTa(taVo);
		int page = (taVo.getPage()-1)*taVo.getLimit();
		taVo.setPage(page);
		List<Ta> findTa = taDao.findTa(taVo);
		map.put("count", count);
		map.put("data", findTa);
		map.put("msg", "");
		map.put("code", 0);
		return map;
	}
	
	@Override
	public void ajaxUploadExcel(InputStream in, MultipartFile file) throws Exception {
		List<List<Object>> listob = ExcelUtil.getBankListByExcel(in, file.getOriginalFilename());
		List<Ta> taList = new ArrayList<Ta>();
		for (int i = 0; i < listob.size(); i++) {
			try {
				List<Object> objects = listob.get(i);
				Ta ta = new Ta();
				ta.setFundTradeId(IDUtil.getUUID());
				ta.setFundTradeNo("ZJJY-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +"-" + IDUtil.getUUIDA());
				ta.setFundId(String.valueOf(objects.get(0)));
				ta.setFundCode(String.valueOf(objects.get(1)));
				ta.setFundName(String.valueOf(objects.get(2)));
				ta.setFundTradeType(String.valueOf(objects.get(3)));
				ta.setAccountId(String.valueOf(objects.get(4)));
				ta.setAccountNo(String.valueOf(objects.get(5)));
				ta.setAccountName(String.valueOf(objects.get(6)));
				ta.setTradeFlag(new Integer(String.valueOf(objects.get(7).toString())));
				ta.setTradePrice(new BigDecimal(String.valueOf(objects.get(8).toString())));
				ta.setTradeDate(new Date());
				ta.setShares(new Integer(String.valueOf(objects.get(9).toString())));
				ta.setTurnover(new BigDecimal(String.valueOf(objects.get(10).toString())));
				ta.setStampTax(new BigDecimal(String.valueOf(objects.get(11).toString())));
				ta.setManagementFees(new BigDecimal(String.valueOf(objects.get(12).toString())));
				ta.setTransferFee(new BigDecimal(String.valueOf(objects.get(13).toString())));
				ta.setCommission(new BigDecimal(String.valueOf(objects.get(14).toString())));
				ta.setBrokerage(new BigDecimal(String.valueOf(objects.get(15).toString())));
				ta.setTotal(new BigDecimal(String.valueOf(objects.get(16).toString())));
				ta.setSecurityInterest(new BigDecimal(String.valueOf(objects.get(17).toString())));
				ta.setTradeStatus(new Integer(String.valueOf(objects.get(18).toString())));
				taList.add(ta);
			} catch (Exception e) {
				System.out.println(e);
				continue;
			}
		}
		taDao.insertTa(taList);
	}

	@Override
	public Map<String, Object> find(String ta) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Ta> find = taDao.find(ta);
		map.put("data", find);
		map.put("code", 0);
		return map;
	}

	@Override
	public int updateFundTrade(String fundTradeId) {
		return taDao.updateFundTrade(fundTradeId);
	}

	@Override
	public int fanjiesuan(String fundTradeId) {
		return taDao.fanjiesuan(fundTradeId);
	}

	@Override
	public int insertCapitalTransfer(Ta ta) {
		int result = 0;
		CapitalTransfer capitalTransfer = new CapitalTransfer();
		capitalTransfer.setCapitalTransferId(IDUtil.getUUID());
		capitalTransfer.setCapitalTransferNo("ZJDB-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +"-" + IDUtil.getUUIDA());
		capitalTransfer.setFundId(ta.getFundId());
		capitalTransfer.setFundCode(ta.getFundCode());
		capitalTransfer.setFundName(ta.getFundName());
		capitalTransfer.setAccountId(ta.getAccountId());
		capitalTransfer.setAccountNo(ta.getAccountNo());
		capitalTransfer.setAccountName(ta.getAccountName());
		capitalTransfer.setTransferAmount(ta.getTurnover());
		capitalTransfer.setTransferDate(new Date());
		capitalTransfer.setTransferFlag(ta.getTradeFlag());
		if (ta.getFundTradeType().equals("1")) {
			capitalTransfer.setTransferType("申购调拨");
		}else if (ta.getFundTradeType().equals("2")) {
			capitalTransfer.setTransferType("认购调拨");
		}else {
			capitalTransfer.setTransferType("赎回调拨");
		}
		capitalTransfer.setBusinessId(ta.getFundTradeId());
		result = taDao.insertCapitalTransfer(capitalTransfer);
		return result;
	}

	@Override
	public int insertFundinventory(Ta ta) {
		int result = 0;
		int i = taDao.countFundinventory(ta);
		if (ta.getFundTradeType().equals("1") || ta.getFundTradeType().equals("2")) {
			if (i == 0) {
				Fundinventory fundinventory = new Fundinventory();
				Fund fund = taDao.selectFund(ta);
				fundinventory.setFundInventoryId(IDUtil.getUUID());
				fundinventory.setFundInventoryNo("JJKC-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +"-" + IDUtil.getUUIDA());
				fundinventory.setFundId(fund.getFundId());
				fundinventory.setFundCode(fund.getFundNo());
				fundinventory.setFundName(fund.getFundName());
				fundinventory.setNav(fund.getIOPV());
				fundinventory.setShare(Integer.valueOf(fund.getFundScale().intValue()));
				fundinventory.setTurnover(fund.getIOPV().multiply(fund.getFundScale()));
				fundinventory.setStatisticalDate(new Date());
				fundinventory.setDescription(fund.getFundType());
				result = taDao.insertFundinventory(fundinventory);
			}else{
				Fundinventory fundinventory = taDao.selectFundinventory(ta);
				fundinventory.setShare(fundinventory.getShare() - ta.getShares());
				fundinventory.setFundId(ta.getFundId());
				result = taDao.updateShare(fundinventory);
			}
		}else{
			if (i == 0) {
				Fundinventory fundinventory = new Fundinventory();
				Fund fund = taDao.selectFund(ta);
				fundinventory.setFundInventoryId(IDUtil.getUUID());
				fundinventory.setFundInventoryNo("JJKC-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +"-" + IDUtil.getUUIDA());
				fundinventory.setFundId(fund.getFundId());
				fundinventory.setFundCode(fund.getFundNo());
				fundinventory.setFundName(fund.getFundName());
				fundinventory.setNav(fund.getIOPV());
				fundinventory.setShare(Integer.valueOf(fund.getFundScale().intValue()));
				fundinventory.setTurnover(fund.getIOPV().multiply(fund.getFundScale()));
				fundinventory.setStatisticalDate(new Date());
				fundinventory.setDescription(fund.getFundType());
				result = taDao.insertFundinventory(fundinventory);
			}else{
				Fundinventory fundinventory = taDao.selectFundinventory(ta);
				fundinventory.setShare(fundinventory.getShare() + ta.getShares());
				fundinventory.setFundId(ta.getFundId());
				result = taDao.updateShare(fundinventory);
			}
		}
		return result;
	}

	@Override
	public int insertCashinventory(Ta ta) {
		int result = 0;
		int i = taDao.countCashinventory(ta);
		if (ta.getFundTradeType().equals("1") || ta.getFundTradeType().equals("2")) {
			if (i == 0) {
				CashInventory cashInventory = new CashInventory();
				cashInventory.setCachInventoryId(IDUtil.getUUID());
				cashInventory.setCachInventoryNo("XJKC-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +"-" + IDUtil.getUUIDA());
				cashInventory.setFundId(ta.getFundId());
				cashInventory.setFundNo(ta.getFundCode());
				cashInventory.setFundName(ta.getFundName());
				cashInventory.setAccountId(ta.getAccountId());
				cashInventory.setAccountNo(ta.getAccountNo());
				cashInventory.setAccountName(ta.getAccountName());
				cashInventory.setCashBalance(ta.getTurnover());
				cashInventory.setStatisticalDate(new Date());
				cashInventory.setDescription("");
				result = taDao.insertCashinventory(cashInventory);
			}else{
				CashInventory cashInventory = taDao.selectCashinventory(ta);
				cashInventory.setCashBalance(cashInventory.getCashBalance().add(ta.getTurnover()));
				cashInventory.setFundId(ta.getFundId());
				result = taDao.updatecashBalance(cashInventory);
			}
		}else{
			if (i == 0) {
				CashInventory cashInventory = new CashInventory();
				cashInventory.setCachInventoryId(IDUtil.getUUID());
				cashInventory.setCachInventoryNo("XJKC-"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) +"-" + IDUtil.getUUIDA());
				cashInventory.setFundId(ta.getFundId());
				cashInventory.setFundNo(ta.getFundCode());
				cashInventory.setFundName(ta.getFundName());
				cashInventory.setAccountId(ta.getAccountId());
				cashInventory.setAccountNo(ta.getAccountNo());
				cashInventory.setAccountName(ta.getAccountName());
				cashInventory.setCashBalance(ta.getTurnover());
				cashInventory.setStatisticalDate(new Date());
				cashInventory.setDescription("");
				result = taDao.insertCashinventory(cashInventory);
			}else{
				CashInventory cashInventory = taDao.selectCashinventory(ta);
				cashInventory.setCashBalance(cashInventory.getCashBalance().subtract(ta.getTurnover()));
				cashInventory.setFundId(ta.getFundId());
				result = taDao.updatecashBalance(cashInventory);
			}
		}
		return result;
	}


	
}
